<?php


require_once 'include/FOREIGN/Forms.php';
require_once 'include/Database/LoginDatabase.php';
require_once 'include/Session.php';


class Page
{
   protected $_dbLogin;
   protected $_session;
   protected $_content;


   function __construct($dbLogin, $session) 
   {
      $this->_dbLogin = $dbLogin;   
      $this->_session = $session;   
   }

   public function setDbLogin($db)
   {
      $this->_dbLogin = $db;   
   }

   public function getDbKey()
   {
      return $this->_dbLogin->getKey();
   }


   public function getDbLoginTableName()
   {
      return $this->_dbLogin->getTableName();
   }


   public function getDbLoginTableFields()
   {
      return $this->_dbLogin->getTableFields();
   }


   public function getSessionName()
   {
      return $this->_session->getName();
   }


   public function getSessionPassword()
   {
      return $this->_session->getPassword();
   }


   public function setSessionName($n)
   {
      $this->_session->setName($n);
   }


   public function setSessionPassword($p)
   {
      $this->_session->setPassword($p);
   }


   public function getSessionStringName()
   {
      return $this->_session->getStringName();
   }


   public function getSessionStringPassword()
   {
      return $this->_session->getStringPassword();
   }


   public function close()
   {
      $this->_dbLogin->close();
      $this->_session->close();
   }


   public function logout()
   {
      $this->_dbLogin->close();
      $this->_session->close();
      unset($this->_content);
      unset($this);
   }


   public function queryLogin($q)
   {
      return $this->_dbLogin->query($q);
   }


   public function strip($q)
   {
      return $this->_dbLogin->real_escape_string($q);
   }


   public function validateSession()
   { 
      if (!sessionIsSet($this))
      { 
         return false;     //Error class for this to indicate need login???
      }

      else
      {
         $n = $this->_dbLogin->real_escape_string($this->getSessionName());
         $p = $this->_dbLogin->real_escape_string($this->getSessionPassword());
         $query="SELECT * FROM `".$this->getDbLoginTableName()."` WHERE `ID`=('$n') AND DES_DECRYPT(`Password`,'".$this->getDbKey()."')= ('$p') LIMIT 1";
         $result=$this->_dbLogin->query($query);
         if($result->num_rows == 1)
         {   
            return true;
         }
         else 
         { 
           return false;
         }
      }
   }


   public function setSession($n, $p)
   {
      $this->setSessionName($n);
      $this->setSessionPassword($p);
   }


   public function pageLogin($n, $p)
   {
      $n = $this->strip($n);
      $p = $this->strip($p);
      $valid = $this->_dbLogin->login($n, $p, "ID", "Password");
      if($valid)
      {   
         $this->setSession($n, $p);
         return true;
      }
      else 
      {
        $this->setSession(null, null);
        return false;
      }
   }


   public function register($first, $last, $mi, $email)
   {
      $tempQuery='INSERT INTO `'.$this->getDbLoginTableName().'` '.$this->getDbLoginTableFields().' VALUES (\''.$this->strip($_POST[$this->getSessionStringName()]).'\',\''.$this->strip($_POST[$first]).'\',\''.$this->strip($_POST[$last]).'\',\''.$this->strip($_POST[$mi]).'\',\''.$this->strip($_POST[$email]).'\',DES_ENCRYPT(\''.$this->strip($_POST[$this->getSessionStringPassword()]).'\',\''.$this->getDbKey().'\'));';
      $this->queryLogin($tempQuery);
   }

}


function sessionIsSet($page)
{
   return (isset($_SESSION[$page->getSessionStringPassword()]) && isset($_SESSION[$page->getSessionStringPassword()]));
}


function postIsSet($page)
{
   return (isset($_POST[$page->getSessionStringName()]) && isset($_POST[$page->getSessionStringPassword()]));
}


function getPostName($page)
{
   return $_POST[$page->getSessionStringName()];
}


function getPostPassword($page)
{
   return $_POST[$page->getSessionStringPassword()];
}

function getPost($var)
{
   return $_POST[$var];
}


function printLogin($page)
{
   ?>
   <div style="
      position:relative;
      left:expression((document.body.clientWidth/2)-239); 
      top:expression((document.body.clientHeight/2)-98);
      width: 20%; 
      height: 800;"
   />
   <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" align=right> 
      User Name:<input type="text" name="<?php echo $page->getSessionStringName(); ?>"><br>&nbsp;&nbsp;
      Password:<input type="password" name="<?php echo $page->getSessionStringPassword(); ?>"><br>  
      <input type="submit" name="submit" value="Login"> 
   </form><br>
   Need to <a href="register.php">register</a>?
   <?php
}




?>

